package com.bqtv.sport.adapter

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.DynamicDrawableSpan
import android.text.style.ForegroundColorSpan
import android.text.style.ImageSpan
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseMultiItemAdapter
import com.bqtv.sport.databinding.ItemLiveEnterMsgBinding
import com.bqtv.sport.databinding.ItemLiveMsgBinding
import com.bqtv.sport.databinding.ItemLiveSystemMsgBinding
import com.tencent.qcloud.tuikit.timcommon.TIMCommonService
import com.tencent.qcloud.tuikit.timcommon.bean.Emoji
import com.tencent.qcloud.tuikit.timcommon.component.face.FaceManager
import java.util.regex.Pattern

class LiveChatAdapter :
    BaseMultiItemAdapter<String>() {
    class SystemVH(val viewBinding: ItemLiveSystemMsgBinding) : RecyclerView.ViewHolder(viewBinding.root)
    class ItemVH(val viewBinding: ItemLiveMsgBinding) : RecyclerView.ViewHolder(viewBinding.root)
    class EnterVH(val viewBinding: ItemLiveEnterMsgBinding) : RecyclerView.ViewHolder(viewBinding.root)

    // 在 init 初始化的时候，添加多类型
    init {
        addItemType(ITEM_TYPE, object : OnMultiItemAdapterListener<String, ItemVH> { // 类型 1
            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): ItemVH {
                // 创建 viewholder
                val viewBinding = ItemLiveMsgBinding.inflate(LayoutInflater.from(context), parent, false)
                return ItemVH(viewBinding)
            }

            override fun onBind(holder: ItemVH, position: Int, item: String?) {
                // 绑定 item 数据
                var str:String  = "";
                var nickName:String  = "昵称";
                str=nickName+":"
                str = str + ":123456"
                val msg: SpannableStringBuilder = handlerEmojiText(str)
                val span = ForegroundColorSpan(Color.parseColor("#2C9CED"))
                msg.setSpan(span, 1, 1 + nickName.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)

                holder.viewBinding.tvMsg.setText(msg)

            }
        }).addItemType(SYSTEM_TYPE, object : OnMultiItemAdapterListener<String, SystemVH> { // 类型 2
            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): SystemVH {
                // 创建 viewholder
                val viewBinding = ItemLiveSystemMsgBinding.inflate(LayoutInflater.from(context), parent, false)
                return SystemVH(viewBinding)
            }

            override fun onBind(holder: SystemVH, position: Int, item: String?) {
                // 绑定 item 数据
            }

        }).addItemType(ENTER_TYPE, object : OnMultiItemAdapterListener<String, EnterVH> { // 类型 2
            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): EnterVH {
                // 创建 viewholder
                val viewBinding = ItemLiveEnterMsgBinding.inflate(LayoutInflater.from(context), parent, false)
                return EnterVH(viewBinding)
            }

            override fun onBind(holder: EnterVH, position: Int, item: String?) {
                // 绑定 item 数据
            }

        }).onItemViewType { position, list -> // 根据数据，返回对应的 ItemViewType
            if (position==0) {
                SYSTEM_TYPE
            } else if (position==1) {
                ITEM_TYPE
            } else {
                ENTER_TYPE
            }
        }
    }

    fun handlerEmojiText(content: CharSequence): SpannableStringBuilder {
        val spannable: SpannableStringBuilder
        spannable = SpannableStringBuilder(content)
        val regex = "\\[(\\S+?)\\]"
        val p = Pattern.compile(regex)
        val m = p.matcher(content)
        var imageFound = false
        while (m.find()) {
            val emojiName = m.group()
            val emoji: Emoji? = FaceManager.getEmojiMap().get(emojiName)
            if (emoji != null) {
                val bitmap: Bitmap? = emoji.getIcon()
                if (bitmap != null) {
                    imageFound = true

                    val imageSpan = ImageSpan(TIMCommonService.getAppContext(), bitmap)
                    spannable.setSpan(
                        imageSpan,
                        m.start(),
                        m.end(),
                        Spannable.SPAN_INCLUSIVE_EXCLUSIVE
                    )
                }
            }
        }
        return spannable
    }

    companion object {
        private const val ITEM_TYPE = 0
        private const val SYSTEM_TYPE = 1
        private const val ENTER_TYPE = 2
    }
}